<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 贪心算法：只计算上涨的利润，下跌不计算不计算
      /**
       * @param {number[]} prices
       * @return {number}
       */
      var maxProfit = function (prices) {
        var total = 0;
        for (var i = 0; i < prices.length - 1; i++) {
          if (prices[i] < prices[i + 1]) {
            total += prices[i + 1] - prices[i];
          }
        }
        return total;
      };

      var prices = [7, 1, 5, 3, 6, 4];
      console.log(maxProfit(prices));
    </script>
  </body>
</html>
